<?PHP
//JMR::Parche para arreglar que en hotmail no se ven los banners//
$url_base = "";
if(isset($set_domain_base_url)){
	$url_base = $set_domain_base_url; 	
}

//END parche//

//JMR: descomentar estos campos para tomar mediciones de cuanto tarda en ejecutarse el while, y cada elemento 
//$tiempo=0;
//$time_start=0;
//$time_end=0;
//Marcas de Tiempo
if(isset($time_start)){
	logMsg("AD_GET","Tomamos Marca de Tiempo en  el modulo ad_get.php");
	$time_start = microtime(true);
}

$teller = 0;$c=0;$ok=0;$sql_banner="";
$catid=getParamInt("catid","$catid");
global $banner_tbl, $shown_banner, $set_banner;

if ($set_banner)
{
if (!isset($shown_banner))
	$shown_banner = 0;

if ($catbanner AND $catid>0)
{
	$sql_banner = "SELECT filename,bid,run_for_hits,hits,html_banner FROM $banner_tbl where bid<>$shown_banner AND run_for_hits>0 AND run_for_clicks>0 AND run_for_days>0 and status='1' AND run_in_group IS NOT null AND run_in_group <> '' AND run_in_group REGEXP '(^|:)$catid([^0-9]|$)'"; 
	$ok=1;	
}
elseif ($section)
{
	$sql_banner = "SELECT filename,bid,run_for_hits,hits,html_banner FROM $banner_tbl where bid<>$shown_banner AND run_for_hits>0 AND run_for_clicks>0 AND run_for_days>0 and status='1' AND (run_in_group = null OR run_in_group='') AND section='$section'";	
	$ok=1;
}

if ($ok)
{
	$res_banner=q($sql_banner);
	$num_banner=mysql_num_rows($res_banner);
	
	while ($row=mysql_fetch_array($res_banner))
	{
		$bannerarray[$c] = $row["filename"];
		$bannerarray_html[$c] = $row["html_banner"];
		$run_for_hits = $row["run_for_hits"];
		
		$bid_nb[$c] = $row["bid"];
		$hits = $row["hits"];
		
		if (!$hits)
			q("update $banner_tbl set hits = 0, days = 0, clicks = 0, y_day=8 where bid = $bid_nb[$c]");	
		$c++;
	}
	
}     

if ($num_banner>0)
{
	$rand_nb = rand(0,$num_banner-1);
	$cur_banner = $bannerarray[$rand_nb];
	$cur_html = $bannerarray_html[$rand_nb];
	
	$cur_banner_bid = $bid_nb[$rand_nb];

	if ($cur_banner)
	{
		$lBanner="<a href='/ad_click.php?bid=$cur_banner_bid' target='new'>";
		if ($cur_html)
			$lBanner.=$cur_html;
		else
			$lBanner.="<img src='".$url_base."/images/banners/$cur_banner' border='0' />";
		$lBanner.="</a>";
		
		echo $lBanner;
		
		//$shown_banner = $cur_banner_bid;
		
		q("update $banner_tbl set hits=hits+1,run_for_hits=run_for_hits-1 where bid=$cur_banner_bid");
		$t_day = date("d");
		q("update $banner_tbl set run_for_days=run_for_days-1 where bid=$cur_banner_bid AND y_day <> $t_day");
		q("update $banner_tbl set days=days+1,y_day=$t_day where bid=$cur_banner_bid AND y_day <> $t_day");
		
		
	}
}
}
//Marcas de Tiempo
if(isset($tiempo)){
	$time_end = microtime(true);
	$tiempo = $tiempo+($time_end-$time_start); 
	//Marcas de Tiempo 
	logMsg("TIEMPO DEMORADO","Tiempo que tardo el modulo  ad_get:: ".$tiempo." seg");
}	
?>